﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DnD.ViewModels;
using System.Diagnostics;

namespace DnD
{
    public partial class CardEditorPage : PhoneApplicationPage
    {
        public CardEditorPage()
        {

            try
            {

                InitializeComponent();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            

            base.OnNavigatedTo(e);
            string cardIDString;
            if (NavigationContext.QueryString.TryGetValue("cardID", out cardIDString))
            {
                int cardID;
                if (int.TryParse(cardIDString, out cardID))
                {
                    DataContext = new CardViewModel(cardID, this,true);
                }

            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            ((CardViewModel)DataContext).clearAppState();
            base.OnBackKeyPress(e);
        }


        private void AppBar_Click(object sender, EventArgs e)
        {
            ((CardViewModel)DataContext).handleMenuRequest(((ApplicationBarIconButton)sender).Text);
        }
    }
}